package www.openteam.com.ui.business;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GestureDetectorCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.base.OtherBaseFragment;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.bean.ProductInfo;
import www.openteam.com.contract.business.UpdateProductInfoContract;
import www.openteam.com.presenter.business.UpdateProductInfoPresenter;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.view.business.UpdateProductInfoView;

/**
 * Created by kylin on 2016/10/26.
 */

public class UpdateProductInfoFragment extends OtherBaseFragment<UpdateProductInfoPresenter>
        implements UpdateProductInfoContract.UpdateProductInfoView {

    private static final String TAG = "BuildPInfoFr";
    private ArrayList<CustomType.DataBean> materialType;
    private MyAdapter materialAdapter;
    @Bind(R.id.product_type)
    Spinner product_type;
    @Bind(R.id.productno)
    EditText productno;
    @Bind(R.id.productname)
    TextView productname;
    @Bind(R.id.customno_product)
    TextView customno_product;
    @Bind(R.id.width_product)
    TextView width_product;
    @Bind(R.id.length_product)
    TextView length_product;
    @Bind(R.id.height_product)
    TextView height_product;
    @Bind(R.id.unit_product_new)
    TextView unit_product_new;
    @Bind(R.id.text_product)
    TextView text_product;
    @Bind(R.id.conver_product)
    TextView conver_product;
    @Bind(R.id.maxstock)
    TextView maxstock;
    @Bind(R.id.minstock)
    TextView minstock;
    @Bind(R.id.returnButton1)
    Button returnButton1;
    private Context mContext;
    public ButtonReturn buttonReturn;
    private Map<Integer, String> typeName;
    private ProductInfo.DataEntity productInfo;
    private int type = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        materialType = getArguments().getParcelableArrayList("customTypes");
        productInfo = getArguments().getParcelable("queryCustom");
    }


    @Override
    public int getLayoutId() {
        return R.layout.product_entry_detail;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }


    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected UpdateProductInfoPresenter loadPresenter() {
        return new UpdateProductInfoPresenter();
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        returnButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mPresenter.updateProductInfo(getCode(), getName(), getTypeName().get(0), getCCode(),
                        getLength(), getWidth(), getHeight(),
                        getUnit(), getMax(), getTypeName().get(1), getConver(), getText(), getMin());

            }
        });
    }

    private Map<Integer, String> returnCode(ArrayList<CustomType.DataBean> cus, int i) {
        String code = "1001";
        String name = "咭盒(PB)";
        Map<Integer, String> temp = new HashMap<Integer, String>();
        code = cus.get(i).getCode();
        name = cus.get(i).getName();
        Log.d(TAG, "onClick: " + code + ":" + name);
        temp.put(1, name);
        temp.put(0, code);
        return temp;
    }


    protected void initView() {
        Log.d(TAG, "initView: 4");
        for (int i = 0; i < materialType.size(); i++) {
            if (materialType.get(i).getCode().equals(productInfo.getType())) {
                type = i;

            }

        }
        materialAdapter = new MyAdapter(materialType, getActivity());
        product_type.setAdapter(materialAdapter);
        product_type.setSelection(type);
        productno.setText(productInfo.getCode());
        productno.setEnabled(false);
        productname.setText(productInfo.getName());
        customno_product.setText(productInfo.getCustCode());
        length_product.setText(productInfo.getLength());
        width_product.setText(productInfo.getWidth());
        height_product.setText(productInfo.getHeight());
        unit_product_new.setText(productInfo.getUnit());
        text_product.setText(productInfo.getContentpage() + "");
        conver_product.setText(productInfo.getCoverPage() + "");
        maxstock.setText(productInfo.getMaxstock());
        minstock.setText(productInfo.getMinstock());
        returnButton1.setText("更新");
    }

    @Override
    public void onRefresh() {

    }


    public interface ButtonReturn {
        void clickReturn(String code);
    }


    public void buttonSetOnclick(ButtonReturn buttonReturn) {
        this.buttonReturn = buttonReturn;
    }

    public boolean checkNull() {

        boolean isNull = false;


        if (getCode().isEmpty()) {
            isNull = true;
            productno.setError("产品编号不能为空！");
        } else if (getName().isEmpty()) {
            isNull = true;
            productname.setError("产品名称不能为空");
        } else if (getCCode().isEmpty()) {
            isNull = true;
            customno_product.setError("客户编号不能为空");
        } else if (getLength().isEmpty()) {
            isNull = true;
            length_product.setError("长度不能为空");
        } else if (getWidth().isEmpty()) {
            isNull = true;
            width_product.setError("宽度不能为空");
        } else if (getHeight().isEmpty()) {
            isNull = true;
            height_product.setError("高度不能为空");
        } else if (getText().isEmpty()) {
            isNull = true;
            text_product.setError("内文不能为空");
        } else if (getConver().isEmpty()) {
            isNull = true;
            conver_product.setError("封面不能为空");
        }

        if (!isNull) {
            createProgressDialog(getResources().getString(R.string.waiting));
        }
        return isNull;

    }


    @Override
    public String getCode() {
        return productno.getText().toString();
    }

    @Override
    public String getName() {
        return productname.getText().toString();
    }

    @Override
    public String getCCode() {
        return customno_product.getText().toString();
    }

    @Override
    public String getLength() {
        return length_product.getText().toString();
    }

    @Override
    public String getWidth() {
        return width_product.getText().toString();
    }

    @Override
    public String getHeight() {
        return height_product.getText().toString();
    }

    @Override
    public String getUnit() {
        return unit_product_new.getText().toString();
    }

    @Override
    public String getText() {
        return text_product.getText().toString();
    }

    @Override
    public String getConver() {
        return conver_product.getText().toString();
    }

    @Override
    public String getMax() {
        return maxstock.getText().toString();
    }

    @Override
    public String getMin() {
        return minstock.getText().toString();
    }

    @Override
    public Map<Integer, String> getTypeName() {
        typeName = new HashMap<Integer, String>();

        typeName = returnCode(materialType, product_type.getSelectedItemPosition());
        return typeName;
    }


    @Override
    public void updatePIData(InsertStatus insertStatus) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(getContext(), "更新成功");
        if (buttonReturn != null) {
            buttonReturn.clickReturn(productno.getText().toString());
        }
    }

    @Override
    public void hidePIData(Throwable throwable) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(getContext(), "插入失败");
        Log.d(TAG, "hidePIData: " + throwable);
    }

    class MyAdapter extends BaseAdapter {

        List<CustomType.DataBean> mList = null;
        Context mContext = null;
        String code = null;

        public MyAdapter(List mList, Context mContext) {
            this.mList = mList;
            this.mContext = mContext;

        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
            convertView = _LayoutInflater.inflate(R.layout.item_user, null);
            if (convertView != null) {
                TextView tv = (TextView) convertView.findViewById(R.id.spinner_item);
                tv.setText(mList.get(position).getName());
            }
            return convertView;
        }
    }
}


